package com.kj.xuyuan.login;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.OnClick;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.widget.WebViewActivity;

public class OneKeyLoginActivity extends BaseActivity {

    @BindView(R.id.tv_one_key_login)
    TextView tvOneKeyLogin;
    @BindView(R.id.tv_other_login)
    TextView tvOtherLogin;
    @BindView(R.id.iv_wx_login)
    ImageView ivWxLogin;
    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.tv_login_explain)
    TextView tvLoginExplain;

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_one_key_login;
    }

    @Override
    protected void initView() {
        setAgreementTextClick();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return -1;
    }

    private void setAgreementTextClick() {
        String content = tvAgreement.getText().toString();
        SpannableString spannableString = new SpannableString(content);
        MyClickText myClickText = new MyClickText(context, 0);
        MyClickText myClickText2 = new MyClickText(context, 1);
        //传入一个MyClickText，并且继承ClickableSpan（必须这样写啊）
        spannableString.setSpan(myClickText, content.indexOf("《用户协议》"),
                content.indexOf("《用户协议》") + 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(myClickText2, content.indexOf("《隐私政策》"), content.indexOf("《隐私政策》") + 6
                , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvAgreement.setText(spannableString);
        tvAgreement.setHighlightColor(getResources().getColor(R.color.transparent)); //设置点击后的颜色为透明
        tvAgreement.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @OnClick({R.id.tv_one_key_login, R.id.tv_other_login, R.id.iv_wx_login,R.id.tv_login_explain})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_one_key_login:
                break;
            case R.id.tv_other_login:
                PhoneLoginActivity.actionStar(context);
                break;
            case R.id.iv_wx_login:
                break;
            case R.id.tv_login_explain:
                LoginExplainActivity.actionStar(context);
                break;
        }
    }

    private class MyClickText extends ClickableSpan {
        private Context context;
        private int type = 0;

        public MyClickText(Activity mainActivity, int type) {
            this.context = mainActivity;
            this.type = type;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            //设置文本的颜色
            ds.setColor(context.getResources().getColor(R.color.pink));
            //超链接形式的下划线，false 表示不显示下划线，true表示显示下划线
            ds.setUnderlineText(false);
        }

        @Override
        public void onClick(View widget) {
            Bundle bundle = new Bundle();
            if (type == 0) {
                //用户协议
                WebViewActivity.actionStar(context,3);
            } else {
                //隐私协议
                WebViewActivity.actionStar(context,4);
            }
        }
    }
}